package com.gary.shiro.entity;

import lombok.Data;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;

/**
 * @author peng_rui
 * @date 2022/3/26
 */
@Getter
@Setter
public class ApiResult<T> implements Serializable {
    private static final long serialVersionUID = -2111905881494585875L;
    private int code;
    private String msg;
    private T data;

    public ApiResult() {
        this.code = 0;
        this.msg = "success";
    }

    public ApiResult(T data) {
        this.code = 0;
        this.msg = "success";
        this.data = data;
    }

    public ApiResult(int code, String msg) {
        this.code = code;
        this.msg = msg;
        this.data = null;
    }

    public ApiResult(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static ApiResult success() {
        return new ApiResult(0, "success");
    }

    public static <T> ApiResult<T> success(T data) {
        return new ApiResult(data);
    }

    public static <T> ApiResult<T> success(String msg, T data) {
        return new ApiResult(0, msg, data);
    }

    public static ApiResult fail(String errorMsg) {
        return new ApiResult(-1, errorMsg);
    }

    public static ApiResult fail(int code, String errorMsg) {
        return new ApiResult(code, errorMsg);
    }

    public static ApiResult fail(Exception e) {
        return new ApiResult(-1, e == null ? "未知错误" : e.getMessage());
    }

    public static ApiResult error(String errorMsg) {
        return new ApiResult(-1, errorMsg);
    }

    public static ApiResult error(int code, String errorMsg) {
        return new ApiResult(code, errorMsg);
    }

    public static ApiResult error(Exception e) {
        return new ApiResult(-1, e == null ? "未知错误" : e.getMessage());
    }

}
